package com.frank.activedatatest.slice;

import com.frank.activedatabus.ActiveDataBus;
import com.frank.activedatabus.Core.Observer;
import com.frank.activedatabus.broadcast.BroadCastManager;
import com.frank.activedatabus.broadcast.BroadcastPublishHelper;
import com.frank.activedatabus.dispatch.ThreadMode;
import com.frank.activedatatest.ResourceTable;
import com.frank.activedatatest.entity.Person;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.Lifecycle;
import ohos.aafwk.abilityjet.activedata.ActiveData;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.event.commonevent.CommonEventData;

public class MainAbilitySlice extends AbilitySlice {

    private ActiveData<Person> personData = new ActiveData<>();
    int count = 0;
    private String mainTest = "test_main";
    private String backgroundTest = "test_background";
    private String stickyTest = "test_sticky";
    private String unstickyTest = "test_unsticky";
    private String delayTest = "test_delay";
    private String lifecycleAwareTest = "test_lifecycleAware";
    private String alwaysTest = "test_always";
    private String foreverTest = "test_forever";
    private String broadcastTest = "test_broadcast";


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        Text mainText = (Text) findComponentById(ResourceTable.Id_text_main);

        ActiveDataBus.get(mainTest, String.class).withBackground(false).withThreadMode(ThreadMode.MAIN).observe(this, s -> {
            mainText.setText("线程名称：" + Thread.currentThread().getName());
        });

        mainText.setClickedListener(component -> {
            getContext().getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() -> {
                ActiveDataBus.get(mainTest).post("主线程");
            });
        });


        Text threadText = (Text) findComponentById(ResourceTable.Id_text_thread);
        ActiveDataBus.get(backgroundTest).withThreadMode(ThreadMode.BACKGROUND).observe(this, t -> {
            threadText.setText("线程名称：" + Thread.currentThread().getName());
        });
        threadText.setClickedListener(component -> {
            ActiveDataBus.get(backgroundTest).post("子线程");
        });

        Button stickyButton = (Button) findComponentById(ResourceTable.Id_button_sticky);
        stickyButton.setClickedListener(component -> {
            ActiveDataBus.get(stickyTest).post("黏性消息");
            ActiveDataBus.get(stickyTest).withSticky(true).observe(this, t -> {
                stickyButton.setText("收到黏性消息");
            });
        });
        Text mainObserver = (Text) findComponentById(ResourceTable.Id_text_main_observer);
        ActiveDataBus.get(stickyTest).withSticky(true).withThreadMode(ThreadMode.MAIN).observe(this, t -> {
            mainObserver.setText("线程名称：" + Thread.currentThread().getName());
        });
        Text backgroundObserver = (Text) findComponentById(ResourceTable.Id_text_background_observer);
        ActiveDataBus.get(stickyTest).withSticky(true).withThreadMode(ThreadMode.BACKGROUND).observe(this, t -> {
            backgroundObserver.setText("线程名称：" + Thread.currentThread().getName());
        });

        Button unstickyButton = (Button) findComponentById(ResourceTable.Id_button_unstiky);
        unstickyButton.setClickedListener(component -> {
            ActiveDataBus.get(unstickyTest).post("非黏性消息");
            ActiveDataBus.get(unstickyTest).withSticky(false).observe(this, t -> {
                unstickyButton.setText("收到非黏性消息");
            });
        });

        Button delayButton = (Button) findComponentById(ResourceTable.Id_button_delay);
        ActiveDataBus.get(delayTest).withSticky(false).observe(this, t -> {
            delayButton.setText("收到延时消息");
            System.out.println("收到延时消息");
        });
        delayButton.setClickedListener(component -> {
            ActiveDataBus.get(delayTest).postDelay("延时消息", 5000);
        });

        Button lifecycleAwareButton = (Button) findComponentById(ResourceTable.Id_button_with_lifecycle_aware);
        TestLifeCycle testLifeCycle = new TestLifeCycle();
        ActiveDataBus.get(lifecycleAwareTest).withSticky(true).observe(testLifeCycle, t -> {
            lifecycleAwareButton.setText("收到生命周期感知消息了吗");
        });
        lifecycleAwareButton.setClickedListener(component -> {
            ActiveDataBus.get(lifecycleAwareTest).post(testLifeCycle, "生命周期感知消息");
        });
        findComponentById(ResourceTable.Id_button_activate_lifecycle).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                testLifeCycle.register();
            }
        });


        Button alwaysButton = (Button) findComponentById(ResourceTable.Id_button_always);
        alwaysButton.setClickedListener(component -> {
            ActiveDataBus.get(alwaysTest).postDelay(this, "发送后台消息", 5000);
        });
        ActiveDataBus.get(alwaysTest).withBackground(true).observe(this, t -> {
            alwaysButton.setText("收到后台消息");
            System.out.println("收到后台消息");
        });

        Button foreverButton = (Button) findComponentById(ResourceTable.Id_button_forever);
        Observer<String> observer = s -> foreverButton.setText("收到forever消息");
        ActiveDataBus.get(foreverTest).observeForever(observer);
        foreverButton.setClickedListener(component -> {
            ActiveDataBus.get(foreverTest).post("发送forever消息");
        });
        findComponentById(ResourceTable.Id_button_remove_observer).setClickedListener(t -> {
            ActiveDataBus.get(foreverTest).removeObserver(observer);
        });

        Button broadcastButton = (Button) findComponentById(ResourceTable.Id_button_broadcast);
        broadcastButton.setClickedListener(component -> {
            IntentParams intentParams = new IntentParams();
            intentParams.setParam("test", "hello");
            new BroadcastPublishHelper.EventPublishBuild(new Intent.OperationBuilder().withAction(broadcastTest).build()).withIntentParams(intentParams).withSticky(false).build().publishEvent();
            BroadCastManager.getInstance().registerCommonEvent(o -> {
                broadcastButton.setText("收到广播消息" + ((CommonEventData) o).getData() + ((CommonEventData) o).getIntent().toUri());
            }, broadcastTest, this);
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
